#include <stdio.h>

const char *str_in_str(const char *str, const char *sub);

int main() {
  const char *str = "C      is quirky,   flawed,    and  an\
enormous     success.       Although accidents of     history\
 surely  helped,    it evidently      satisfied    a    need\
\
    for  a   system  implementation       language      efficient\
    enough    to  displace            assembly   language,\
      yet sufficiently   abstract   and fluent        to describe\
     algorithms   and     interactions    in a      wide    variety\
   of   environments.\
     --       DenniDDDeDeDeDennis     M.          Ritchie";

  printf("%s\n", str_in_str(str, "Dennis"));
  return 0;
}

// 朴素的字符串匹配模式---暴力模式
const char *str_in_str(const char *str, const char *sub) {
  const char *s = str, *pattern = sub;
  int offset = 0;

  // 在主串中搜索
  while (*s != '\0') {
    if (*s == *pattern) {
      s++;
      pattern++;
      offset++;
    } else {
      // 模式串没有完全匹配
      if (*pattern != '\0') {
        s = s - offset + 1; // 主串回退到开始位置并右移一位
        pattern = sub;      // 模式串从头开始
        offset = 0;
      }
    }
    // 模式串完全匹配
    if (*pattern == '\0')
      return s - offset;
  }

  return NULL;
}